#ifndef _Rectangle_H__
#define _Rectangle_H__

#include "Common.h"

#include "Vector2.h"
#include "Vector3.h"

#ifndef  _RECT_H
	#include "Rect.h"
#endif

    struct SERVER_DECL Rectangle
    {
        Real left;
        Real top;
        Real right;
        Real bottom;

	Rectangle()
	{
		left = top = right  = bottom = 0;
	}

	Rectangle(const Real & l,const Real & t,const Real & r, const Real & b)
	{
		left = l,top = t,right = r,bottom = b;
	}
	
	Rectangle(const Rect & rt)
	{
		left = rt.fx,top = rt.fy;
		right = left + rt.fw,bottom = top + rt.fh;
	}

        inline bool inside(Real x, Real y) const { return x >= left && x < right && y >= top && y < bottom; }

	inline bool inside(const Vector2 & pos)	const	{	return inside(pos.x,pos.y);	}
	inline bool inside(const Vector3 & pos) const 	{	return inside(pos.x,pos.y);	}

	inline Rectangle  operator+(const Vector3& pos)
        {
                return Rectangle(left + pos.x,
                                 top + pos.y,
                                 right + pos.x,
                                 bottom + pos.y);
        }


	inline Rectangle  operator-(const Vector3& pos)
        {
                return Rectangle(left - pos.x,
                                 top - pos.y,
                                 right - pos.x,
                                 bottom - pos.y);
        }
       
	inline Rectangle& operator += ( const Vector3& rkVector )
        {       
            left += rkVector.x;
            right += rkVector.x;
            top += rkVector.y;
            bottom += rkVector.y;

            return *this;
        }


        inline Rectangle& operator -= ( const Vector3& rkVector )
        {       
            left -= rkVector.x;
            right -= rkVector.x;
            top -= rkVector.y;
            bottom -= rkVector.y;

            return *this;
        }
	
	inline Rectangle& operator /= (const float & facter)
	{
		left /= facter;
		right /= facter;
		top /= facter;
		bottom /= facter;
		return *this;
	}

	inline bool isNull(void)
	{
		if(left == 0.0f && top == 0.0f && right == 0.0f && bottom == 0.0f)
			return true;
		else
			return false;
	}

	inline bool isZero(void)
	{
		if(bottom <= top && right <= left)
			return true;
		else
			return false;
	}
		
		
    };

    /** Geometric intersection of two rectanglar regions.
     *
     * @remarks Calculates the geometric intersection of two rectangular
     * regions.  Rectangle coordinates must be ([0-N], [0-N]), such that
     * (0,0) is in the upper left hand corner.
     *
     * If the two input rectangles do not intersect, then the result will be
     * a degenerate rectangle, i.e. left >= right or top >= bottom, or both.
     */
    inline Rectangle intersect(const Rectangle& lhs, const Rectangle& rhs)
    {
        Rectangle r;

        r.left   = lhs.left   > rhs.left   ? lhs.left   : rhs.left;
        r.top    = lhs.top    > rhs.top    ? lhs.top    : rhs.top;
        r.right  = lhs.right  < rhs.right  ? lhs.right  : rhs.right;
        r.bottom = lhs.bottom < rhs.bottom ? lhs.bottom : rhs.bottom;

        return r;
    }


#endif // _Rectangle_H__
